﻿
Public Class t01_master_macet

    Dim acr As New ModLibrary.All_Function_Cls
    Public BS As BindingSource

    Private Sub t01_master_macet_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadNow()
    End Sub

    Sub LoadNow()
        BS = Master_macetBindingSource
        'TODO: This line of code loads data into the 'DS_t01_master_macet.master_macet' table. You can move, or remove it, as needed.
        Me.Master_macetTableAdapter.Fill(Me.DS_t01_master_macet.master_macet)
        'TODO: This line of code loads data into the 'DS_t01_master_macet.rute' table. You can move, or remove it, as needed.
        Me.RuteTableAdapter.Fill(Me.DS_t01_master_macet.rute)
        BS.MoveLast()
    End Sub

    Public Function Form_Valid()
        Dim Pesan, Validasi As String
        Pesan = "" : Validasi = ""
        If ID_MACETTextBox.Text = "" Then Pesan += "- ID_Macet Cannot Empty " & vbNewLine
        If ID_RUTEComboBox.Text = "" Then Pesan += "- ID_Rute Month Cannot Empty " & vbNewLine
        If TINGKATComboBox.Text = "" Then Pesan += "- Tingkat Cannot Empty " & vbNewLine
        If Pesan <> "" Then
            Validasi = "Input Data Is Not Correct / Not Complete on : " & vbNewLine & vbNewLine & Pesan
        End If
        Return Validasi
    End Function

    Sub Save()
        Try
            If Form_Valid() = "" Then
                Me.Validate()
                BS.EndEdit()
                Me.TableAdapterManager.UpdateAll(Me.DS_t01_master_macet)
                MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
                LoadNow()
            Else
                MsgBox(Form_Valid, MsgBoxStyle.Critical, "Error Data Saving")
            End If
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Save_Special()
        Try
            Me.Validate()
            BS.EndEdit()
            Me.TableAdapterManager.UpdateAll(Me.DS_t01_master_macet)
            MsgBox("Sukses !", MsgBoxStyle.OkOnly + MsgBoxStyle.Information, "Saving Data")
            LoadNow()
        Catch ex As Exception
            MsgBox(ex.Message.ToString)
        End Try
    End Sub

    Sub Delete()
        If MsgBox("Delete this data ?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Delete Confirmation") = MsgBoxResult.Yes Then
            BS.RemoveCurrent()
            If MsgBox("if you are sure... click [Yes] button to delete and save this changes..", MsgBoxStyle.Information + MsgBoxStyle.OkCancel, "Pending Delete Data") = MsgBoxResult.Ok Then
                If Form_Valid() = "" Then
                    Save()
                Else
                    Save_Special()
                End If
            Else
                acr.MoveNavi("Load", 0, Me, BS)
            End If
        End If
    End Sub

    Private Sub Btn_Save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Save.Click, Btn_Refresh.Click, Btn_Previous.Click, Btn_Next.Click, Btn_Last.Click, Btn_First.Click, Btn_Delete.Click, Btn_Add.Click
        Select Case CType(sender, Button).Name.ToString
            Case "Btn_Save"
                Save()
            Case "Btn_Refresh"
                acr.MoveNavi("Load", 0, Me, BS)
            Case "Btn_Previous"
                acr.MoveNavi("MovePrev", 0, Me, BS)
            Case "Btn_Next"
                acr.MoveNavi("MoveNext", 0, Me, BS)
            Case "Btn_Last"
                acr.MoveNavi("MoveLast", 0, Me, BS)
            Case "Btn_First"
                acr.MoveNavi("MoveFirst", 0, Me, BS)
            Case "Btn_Add"
                acr.MoveNavi("Add", 0, Me, BS)
            Case "Btn_Delete"
                Delete()
        End Select
    End Sub

    Private Sub Master_macetDataGridView_DataError(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewDataErrorEventArgs) Handles Master_macetDataGridView.DataError
        e.Cancel = True
    End Sub

End Class